package com.micai.springboot.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.micai.springboot.base.BaseController;
import com.micai.springboot.dto.MemberDto;
import com.micai.springboot.service.MemberService;
import com.micai.springboot.util.MessageUtils;
import com.micai.springboot.vo.MemberVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/12 12:00
 * @Description: 会员管理
 */
@Controller
@RequestMapping("/admin/member")
public class AdminMemberController extends BaseController {

    @Autowired
    private MemberService memberService;

    /**
     * 分页查询会员列表
     * @param model
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@RequestParam(value="pageNum", defaultValue="1") int pageNum,
                       @RequestParam(value="pageSize", defaultValue="10") int pageSize,
                       @RequestParam(required = false) String searchParam,
                       @RequestParam(required = false) String startDate,
                       @RequestParam(required = false) String endDate, Model model) {
        Page<MemberDto> memberDtos = memberService.getMemberByPage(pageNum, pageSize, searchParam, startDate, endDate);
        PageInfo<MemberDto> pageInfo = new PageInfo<MemberDto>(memberDtos);
        // 参数回显
        model.addAttribute("page", pageInfo);
        model.addAttribute("searchParam", searchParam);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "admin/member/memberList";
    }

    /**
     * 根据ID查询会员信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public Map<String, Object> get(@PathVariable("id") Long id) {
        Map<String, Object> retMap = MessageUtils.success();
        try {
            MemberDto memberDto = memberService.getMemberById(id);
            if (memberDto != null) {
                retMap.put("memberDto", memberDto);
                return retMap;
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 修改会员信息
     *
     * @param memberVo
     * @return
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public Map<String, Object> edit(@ModelAttribute MemberVo memberVo) {
        Map<String, Object> retMap = null;
        try {
            MemberDto memberDto = memberService.getMemberById(memberVo.getId());
            if (memberDto == null) {
                retMap = MessageUtils.error("修改失败");
                return retMap;
            }
            BeanUtils.copyProperties(memberVo, memberDto);
            memberDto.setModifier(getId());
            if (memberService.update(memberDto) == 1) {
                retMap = MessageUtils.success("修改成功");
                return retMap;
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 根据ID批量删除会员信息
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/deleteBatch")
    @ResponseBody
    public Map<String, Object> deleteBatch(@RequestParam("ids[]") List<Long> ids) {
        Map<String, Object> retMap = null;
        try {
            // 删除会员
            if (memberService.deleteBatchIds(ids)) {
                retMap = MessageUtils.success("删除成功");
                return retMap;
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

}
